#include "ssWXGLPanel.h"
#include "tools/ssProcessInfo.h"
#include "shader/ssShaderManager.h"
#include "node/ssNodeFactory.h"
#include "resmgr/ssResManager.h"


ssGLPanel::ssGLPanel(wxWindow* parent, const wxPoint& point, const wxSize& size)
	:wxPanel(parent, wxID_ANY, point, size)
{
	memset(&m_RLastPoint, 0, sizeof(ssWndCoord));
	memset(&m_LLastPoint, 0, sizeof(ssWndCoord));
#ifdef _WIN32
	SS_OPENGL->InitGLDevice((HWND)GetHWND(), 0, size.x, size.y);
#elif SS_USE_X11
	SS_OPENGL->InitGLDevice((GLXDrawable)GetClientAreaWindow(), (Display*)wxGetDisplay(), size.x, size.y);
#endif
	SS_OPENGL->InitGLState(false);
}

ssGLPanel::~ssGLPanel()
{
	StarSeeker::ssSingle<StarSeeker::ssNodeFactory>::delete_instance();
	StarSeeker::ssSingle<StarSeeker::ssResManager>::delete_instance();
	StarSeeker::ssShaderManager::instance()->delete_instance();
	StarSeeker::ssSingle<StarSeeker::ssOpenGLSupport>::delete_instance();
	StarSeeker::ssProcess::delete_instance();
}

BEGIN_EVENT_TABLE(ssGLPanel, wxPanel)
	//EVT_TIMER(ssGLWindow::TIMER_RENDER, ssGLWindow::OnTimerRender)
	//EVT_BUTTON(wxID_ANY, ssFrameWind::OnBtnClick)
	EVT_CLOSE(ssGLPanel::OnClose)
	EVT_KEY_DOWN(ssGLPanel::OnKeyDown)
	EVT_KEY_UP(ssGLPanel::OnKeyUp)
	EVT_LEFT_DOWN(ssGLPanel::OnLButtonDown)
	EVT_RIGHT_DOWN(ssGLPanel::OnRButtonDown)
	EVT_MOTION(ssGLPanel::OnMouseMove)
	EVT_MOUSEWHEEL(ssGLPanel::OnMouseWheel)
	EVT_LEFT_UP(ssGLPanel::OnLeftMouseUp)
	EVT_IDLE(ssGLPanel::OnIdle)
END_EVENT_TABLE()

void ssGLPanel::OnRender()
{
	printf("render...\n");
}

void ssGLPanel::OnClose( wxCloseEvent& ev )
{
	OnExit();
	Destroy();
}

void ssGLPanel::OnIdle( wxIdleEvent& event )
{
	SS_OPENGL->BeginRender();
	OnRender();
	SS_OPENGL->EndRender();
	SS_OPENGL->SwapBuffers();
}

void ssGLPanel::OnRButtonDown( wxMouseEvent& ev )
{
	m_RLastPoint.x = ev.GetX();
	m_RLastPoint.y = ev.GetY();
	//SetFocus();
}

void ssGLPanel::OnLButtonDown(wxMouseEvent& ev)
{
	m_LLastPoint.x = ev.GetX();
	m_LLastPoint.y = ev.GetY();
}
